'''
004 SPACE BATTLE
Controls:       Touch and Tilt
Description:    Tilt up and down to move ship, touch to shoot laser
                kill all space invaders
'''
import pygame
import spyral
import random  


class game004(spyral.scene.Scene):
    def __init__(self, player):
        spyral.scene.Scene.__init__(self)
        self.root_camera = spyral.director.get_camera()
        self.camera = self.root_camera.make_child(layers = [0,1,2,3])
        self.group = spyral.sprite.Group(self.camera)
        
        self.player = player;
        self.timer = Timer.Timer(self.player.current_speed);
        
        self.ship = ship()
        self.scrollingbg = scrollingBG()
        self.invaders = [invader(0), invader(1), invader(2), invader(3)]
        
        self.firing = False;

        self.group.add(self.timer, self.scrollingbg, self.ship,
                       self.invaders[0], self.invaders[1], self.invaders[2], self.invaders[3]);
        
        
    def on_enter(self):
        bg = spyral.util.new_surface(Dic.geom['width'], Dic.geom['height'])
        self.camera.set_background(bg)
        
    def render(self):
        self.group.draw()
        self.root_camera.draw()
        
    def update(self, tick):
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    spyral.director.pop()
                    exit(0)
                if event.key == pygame.K_r:
                    spyral.director.replace(game004(self.player))
                if event.key == pygame.K_w:
                    self.ship.setUp(True);
                elif event.key == pygame.K_s:
                    self.ship.setDown(True);
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_w:
                    self.ship.setUp(False);
                if event.key == pygame.K_s:
                    self.ship.setDown(False);
            if event.type == pygame.MOUSEBUTTONDOWN:
                self.lasers = self.ship.setLasersOn();
                self.group.add(self.lasers[0], self.lasers[1])
                self.firing = True;
            if event.type == pygame.MOUSEBUTTONUP:
                self.ship.setLasersOff();
                self.firing = False;
            
        if (self.firing == True):
            self.checkLasers()

        if (self.timer.timesUp()):
            if (self.winCondition() == False):
                self.player.decHealth();
            else:
                self.player.incCurrentGamesCompleted();
            spyral.director.pop()
        
        self.group.update();
 
    def winCondition(self):
        if (len(self.invaders) == 0):
            return True;
        else:
            return False;
        
    def checkLasers(self):
        for item in self.invaders:
            if (item.rect.top <= self.lasers[0].pos[1] + 30 and self.lasers[1].pos[1] + 30 <= item.rect.bottom):
                self.group.add(item.die())
                self.invaders.remove(item)

class laser(spyral.sprite.Sprite):
    def __init__(self, pos):
        spyral.sprite.Sprite.__init__(self);
        self.image = spyral.util.load_image(Dic.images['004laser']);
        self.layer = 2;
        self.pos = pos;

class scrollingBG(spyral.sprite.Sprite):
    def __init__(self):
        spyral.sprite.Sprite.__init__(self)
        self.image = spyral.util.load_image(Dic.images['004bg'])
        self.pos = (0,0);
        self.layer = 0;
        
    def update(self, tick):
        self.pos = (self.pos[0] - 1, self.pos[1])

class ship(spyral.sprite.Sprite):
    def __init__(self):
        spyral.sprite.Sprite.__init__(self)
        self.img_ship = spyral.util.load_image(Dic.images['004ship'])
        self.img_shipred = spyral.util.load_image(Dic.images['004shipred'])
        self.image = self.img_ship
        self.rect.center = (225, 200);
        self.layer = 2;
        
        self.speed = 10;
        self.up = False;
        self.down = False;
        self.lasers = False;
        
        self.laser1 = None;
        self.laser2 = None;
    
    def update(self, tick):
        if (self.up == True):
            self.rect.center = (self.rect.center[0], self.rect.center[1] - self.speed);
            if ((self.laser1 != None) and (self.laser2 != None)):
                self.laser1.pos = (self.laser1.pos[0], self.laser1.pos[1] - self.speed);
                self.laser2.pos = (self.laser2.pos[0], self.laser2.pos[1] - self.speed);
        if (self.down == True):
            self.rect.center = (self.rect.center[0], self.rect.center[1] + self.speed);
            if ((self.laser1 != None) and (self.laser2 != None)):
                self.laser1.pos = (self.laser1.pos[0], self.laser1.pos[1] + self.speed);
                self.laser2.pos = (self.laser2.pos[0], self.laser2.pos[1] + self.speed);
    
    def setUp(self, bool):
        self.up = bool;
    
    def setDown(self, bool):
        self.down = bool;

    def setLasersOn(self):
        self.image = self.img_shipred;
        self.laser1 = laser((self.rect.center[0] + 137, self.rect.center[1] - 70))
        self.laser2 = laser((self.rect.center[0] + 103, self.rect.center[1] - 51))
        return [self.laser1, self.laser2]
        
    def setLasersOff(self):
        self.image = self.img_ship;
        self.laser1.kill();
        self.laser2.kill();
        self.laser1 = None;
        self.laser2 = None;
    
class invader(spyral.sprite.Sprite):
    def __init__(self, num):
        spyral.sprite.Sprite.__init__(self);
        self.image = spyral.util.load_image(Dic.images['004invader'])
        self.layer = 1;
        if (num == 0):
            self.rect.center = (900, 80);
        elif (num == 1):
            self.rect.center = (900, 240);
        elif (num == 2):
            self.rect.center = (900, 400);
        elif (num == 3):
            self.rect.center = (900, 560);
            
    def update(self, tick):
        self.rect.center = (self.rect.center[0] - 2, self.rect.center[1])
        
    def die(self):
        self.kill();
        return explosion(self.rect.center);
        
        
        

class explosion(spyral.sprite.Sprite):
    
    def __init__(self, pos):
        spyral.sprite.Sprite.__init__(self);
        self.layer = 2;
        self.img_explosion = spyral.util.load_image(Dic.images['004explosion']);
        self.width = self.img_explosion.get_width() / 18;
        self.height = self.img_explosion.get_height();
        self.image = spyral.util.new_surface(self.width, self.height)
        self.image.blit(self.img_explosion, (0,0), area=pygame.Rect(0,0,self.height,self.width))
        self.rect.center = pos;
        
        self.countup = 0;
    
    def update(self, camera):
        if (self.countup == 17):
            self.kill()
        else:
            self.image.fill(0)
            self.image = self.image.copy();
            self.image.blit(self.img_explosion, (0,0), area=pygame.Rect(self.width * self.countup,0,self.height,self.width))
            self.countup += 1;
        



if __name__ == "__main__":
    import sys
    sys.path.append("C:\Users\Patrick\paybackpigs\paybackpigs")
    from frenzy import Dic
    Dic.setImgFolder('../images/')
    from frenzy import Image
    from frenzy import Timer
    from frenzy import Player
    spyral.init()
    spyral.director.init(Dic.geom['size'], False, 'Payback Pigs', 30, 30)
    spyral.director.push(game004(Player.Player()))
    spyral.director.run()

else:
    import Image
    import Dic
    import Timer;